<template>
    <main class="main-page login-page">
        <div class="login-background">
            <div id="particles-js"></div>
            <div class="animated-shapes">
                <div class="shape shape1"></div>
                <div class="shape shape2"></div>
                <div class="shape shape3"></div>
            </div>
        </div>
        <section class="page-section">
            <div class="container-fluid">
                <div class="grid justify-content-center">
                    <div class="col-12 sm:col-6 md:col-4 comp-grid">
                        <div class="login-card">
                            <div class="login-header">
                                <div class="logo-icon">学</div>
                                <h1 class="text-2xl font-bold text-primary mt-3">学链通</h1>
                                <p class="text-500 mt-2">区块链教育认证平台</p>
                            </div>
                            <div class="login-form">
                                <form ref="observer" tag="form" @submit.prevent="startLogin()">
                                    <div class="form-group">
                                        <span class="p-input-icon-right w-full">
                                            <i class="pi pi-user"></i>
                                            <InputText 
                                                :label="$t('usernameOrEmail')" 
                                                :placeholder="$t('usernameOrEmail')" 
                                                class="w-full login-input" 
                                                v-model.trim="formData.username"  
                                                required="required" 
                                                type="text" 
                                            />
                                        </span>
                                    </div>
                                    <div class="form-group">
                                        <span class="p-input-icon-left w-full">
                                            <i class="pi pi-lock"></i>
                                            <Password 
                                                :label="$t('password')" 
                                                inputClass="w-full login-input" 
                                                :feedback="false" 
                                                toggleMask 
                                                class="w-full" 
                                                :placeholder="$t('password')" 
                                                required="required" 
                                                v-model="formData.password" 
                                            />
                                        </span>
                                    </div>
                                    <div class="flex justify-content-between align-items-center my-3">
                                        <div class="flex align-items-center">
                                            <Checkbox 
                                                class="mr-2" 
                                                inputId="rememberme" 
                                                name="rememberme" 
                                                :binary="true" 
                                                v-model="rememberUser" 
                                            />
                                            <label class="text-sm text-500" for="rememberme">{{ $t('rememberMe') }}</label>
                                        </div>
                                        <router-link to="/index/forgotpassword">
                                            <Button class="p-button-text" :label="$t('resetPassword')" />
                                        </router-link>
                                    </div>
                                    <Message 
                                        v-if="loginErrorMsg" 
                                        severity="error" 
                                        :closable="true" 
                                        @close="loginErrorMsg=null"
                                        class="mb-3"
                                    >
                                        {{ loginErrorMsg.toString() }}
                                    </Message>
                                    <div class="text-center">
                                        <Button 
                                            :label="$t('login')"  
                                            :loading="loading" 
                                            icon="pi pi-lock-open" 
                                            class="p-button-lg p-button-raised w-full login-button" 
                                            type="submit"
                                        > 
                                        </Button>
                                    </div>
                                </form>
                            </div>
                            <div class="login-footer mt-4">
                                <div class="grid gap-3 align-items-center justify-content-between">
                                    <div class="col text-500 font-bold text-sm">
                                        {{ $t('dontHaveAnAccount') }}
                                    </div>
                                    <div class="col-fixed text-right">
                                        <router-link to="/index/employer-apply">
                                            <Button 
                                                icon="pi pi-building" 
                                                :label="$t('employerApply')" 
                                                class="p-button-warning mr-2 register-button" 
                                            />
                                        </router-link>
                                        <router-link to="/index/teacher-register">
                                            <Button 
                                                icon="pi pi-user" 
                                                :label="$t('teacherRegister')" 
                                                class="p-button-info mr-2 register-button" 
                                            />
                                        </router-link>
                                        <router-link to="/index/student-register">
                                            <Button 
                                                icon="pi pi-user" 
                                                :label="$t('studentRegister')" 
                                                class="p-button-success register-button" 
                                            />
                                        </router-link>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>
</template>

<script setup>
	import {  ref, reactive, onMounted } from 'vue';
	import { useApp } from 'src/composables/app';
	import { useAuth } from 'src/composables/auth';
	import { useRoute, useRouter } from 'vue-router';
	
	// 引入 particles.js
	import 'particles.js';
	
	const props = defineProps({
		pageName: {
			type: String,
			default: 'index',
		},
		routeName: {
			type: String,
			default: 'index',
		},
		isSubPage: {
			type : Boolean,
			default : false,
		},
	});
	const auth = useAuth();
	const route = useRoute();
	const router = useRouter();
	const app = useApp();
	const formData = reactive({
		username: '',
		password:'',
	});
	const loading = ref(false);
	const pageReady = ref(true);
	const loginErrorMsg = ref('');
	const rememberUser = ref(false);
	async function startLogin (){
		try{
			loading.value = true;
			loginErrorMsg.value = '';
			let url = "/auth/login";
			let payload = {
				formData,
				rememberUser:rememberUser.value,
				url
			};
			const loginData = await auth.login(payload);
			loading.value = false;
			if (loginData.token) {
				let nextUrl = route.query.nexturl || '/home'
				router.go(nextUrl);
			}
			else{
				router.push(loginData.nextpage);
			}
		}
		catch(error){
			loading.value = false;
			loginErrorMsg.value = error.request?.response || "Unable to establish connection...";
		}
	}
	
	onMounted(() => {
		// 确保 particles.js 已加载
		if (typeof window.particlesJS !== 'undefined') {
			window.particlesJS('particles-js', {
				particles: {
					number: {
						value: 100,
						density: {
							enable: true,
							value_area: 800
						}
					},
					color: {
						value: ['#4d7cfe', '#6c63ff', '#3ae374']
					},
					shape: {
						type: 'circle',
						stroke: {
							width: 0,
							color: '#000000'
						}
					},
					opacity: {
						value: 0.6,
						random: true,
						anim: {
							enable: true,
							speed: 1,
							opacity_min: 0.1,
							sync: false
						}
					},
					size: {
						value: 4,
						random: true,
						anim: {
							enable: true,
							speed: 2,
							size_min: 0.1,
							sync: false
						}
					},
					line_linked: {
						enable: true,
						distance: 150,
						color: '#4d7cfe',
						opacity: 0.4,
						width: 1
					},
					move: {
						enable: true,
						speed: 3,
						direction: 'none',
						random: true,
						straight: false,
						out_mode: 'out',
						bounce: false,
						attract: {
							enable: true,
							rotateX: 600,
							rotateY: 1200
						}
					}
				},
				interactivity: {
					detect_on: 'canvas',
					events: {
						onhover: {
							enable: true,
							mode: 'grab'
						},
						onclick: {
							enable: true,
							mode: 'push'
						},
						resize: true
					},
					modes: {
						grab: {
							distance: 180,
							line_linked: {
								opacity: 0.8
							}
						},
						push: {
							particles_nb: 6
						}
					}
				},
				retina_detect: true
			});
		} else {
			console.warn('particles.js is not loaded');
		}
	});
	
</script>

<style lang="scss">
:root {
    --primary: #4d7cfe;
    --secondary: #6c63ff;
    --accent: #3ae374;
    --light: #f9fafe;
    --dark: #2c3e50;
    --gradient: linear-gradient(135deg, #6c63ff, #4d7cfe);
}

.login-page {
    min-height: 100vh;
    position: relative;
    overflow: hidden;
}

.login-background {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 0;
}

#particles-js {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: 1;
    pointer-events: none;
}

.animated-shapes {
    position: absolute;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

.shape {
    position: absolute;
    background: linear-gradient(45deg, var(--primary), var(--secondary));
    border-radius: 50%;
    opacity: 0.1;
    animation: float 15s infinite ease-in-out;

    &.shape1 {
        width: 300px;
        height: 300px;
        top: -150px;
        left: -150px;
        animation-delay: 0s;
    }

    &.shape2 {
        width: 200px;
        height: 200px;
        top: 50%;
        right: -100px;
        animation-delay: -5s;
    }

    &.shape3 {
        width: 150px;
        height: 150px;
        bottom: -75px;
        left: 50%;
        animation-delay: -10s;
    }
}

.login-card {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    padding: 2rem;
    position: relative;
    z-index: 2;
    transform: translateY(0);
    transition: all 0.3s ease;

    &:hover {
        transform: translateY(-5px);
        box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
    }
}

.login-header {
    text-align: center;
    margin-bottom: 2rem;

    .logo-icon {
        width: 60px;
        height: 60px;
        background: var(--gradient);
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto;
        color: white;
        font-size: 24px;
        font-weight: bold;
        box-shadow: 0 4px 15px rgba(77, 124, 254, 0.3);
    }
}

.form-group {
    margin-bottom: 1.5rem;
}

.login-input {
    border-radius: 10px;
    border: 1px solid #e0e0e0;
    transition: all 0.3s ease;

    &:focus {
        border-color: var(--primary);
        box-shadow: 0 0 0 2px rgba(77, 124, 254, 0.2);
    }
}

.login-button {
    background: var(--gradient);
    border: none;
    border-radius: 10px;
    transition: all 0.3s ease;

    &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 15px rgba(77, 124, 254, 0.3);
    }
}

.register-button {
    border-radius: 8px;
    transition: all 0.3s ease;

    &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
    }
}

@keyframes float {
    0%, 100% {
        transform: translateY(0) rotate(0deg);
    }
    50% {
        transform: translateY(-20px) rotate(5deg);
    }
}

// 响应式调整
@media screen and (max-width: 768px) {
    .login-card {
        margin: 1rem;
        padding: 1.5rem;
    }

    .shape {
        opacity: 0.05;
    }
}

// 滚动条样式
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: #f1f1f1;
}

::-webkit-scrollbar-thumb {
    background: var(--primary);
    border-radius: 10px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--secondary);
}
</style>
